struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    struct ListNode* head = NULL, * tail = NULL;
    if (!list1)
    {
        return list2;
    }

    if (!list2)
    {
        return list1;
    }

    while (list1 != NULL && list2 != NULL)
    {
        if (list1->val < list2->val)
        {
            if (tail == NULL)
            {
                head = list1;
                tail = list1;
                list1 = list1->next;
            }
            else
            {
                tail->next = list1;
                list1 = list1->next;
                tail = tail->next;
            }
        }
        else
        {
            if (tail == NULL)
            {
                head = list2;
                tail = list2;
                list2 = list2->next;
            }
            else
            {
                tail->next = list2;
                list2 = list2->next;
                tail = tail->next;
            }
        }
    }
    if (!list1)
    {
        tail->next = list2;
    }
    if (!list2)
    {
        tail->next = list1;
    }
    return head;
}